<?php

namespace backend\models;

use common\server\BasicDataServer;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\AdvertModel;

/**
 * SearchHouseModel represents the model behind the search form about `common\models\HouseModel`.
 */
class SearchAdvertModel extends AdvertModel
{
    public  $mode = 0;


    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = AdvertModel::find();
		$query->joinWith(['adverttype']);
		$query->joinWith(['house']);
		$query->select("ph_adverttype.*,ph_house.house_name,ph_advert.*");
        $query->orderBy(['ph_advert.id'=>SORT_DESC]);
        
        $this->load($params);
        
		
        if(!empty($this->atype)){
            $query->andWhere(['ph_adverttype.tid'=>$this->atype]);
        }
        
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!$this->validate()) {

            return $dataProvider;
        }

        return $dataProvider;
    }
	
}
